﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using MaxLeaf.Common;
using MySql.Data.MySqlClient;
using MaxLeaf.DB;
namespace MaxLeaf.DAL.Web
{
    /// <summary>
    /// 商品属性表，用于扩展商品属性，
    /// </summary>
    public class mary_goods_attrDAL
    {
        public void Update(DBAccess dbOrgCon, DBAccess dbCon, string strgoods_id, Dictionary<string, string> dictMap)
        {
            mary_attributeDAL attrDal = new mary_attributeDAL();
             
            
            foreach (KeyValuePair<string, string> kvp in dictMap)
            {
                string strattr_id = attrDal.Getattr_id(dbCon, kvp.Key);
                if (kvp.Value.Length == 0)
                {
                    Delete(dbCon, strgoods_id, strattr_id);
                }
                else
                {
                    if (kvp.Key.Equals("颜色"))
                    {
                        Delete(dbCon, strgoods_id, strattr_id);

                        string[] arrYS = GetYS(kvp.Value);
                        foreach (string str in arrYS)
                        {
                            if (str.Length > 0)
                            {
                                Add(dbOrgCon, dbCon, strgoods_id, strattr_id, str.Trim());
                            }
                        }
                    }
                    else
                    {
                        Update(dbOrgCon, dbCon, strgoods_id, strattr_id, kvp.Value);
                    }
                }
            }
        }

        private string[] GetYS(string strYS)
        {
            strYS = strYS.Replace('，',',');
            string[] arr = strYS.Split(',');

            return arr;
        }

        private void Update(DBAccess dbOrgCon, DBAccess dbCon, string strgoods_id, string strattr_id, string strattr_value)
        {
            DataRow drData = dbCon.GetTableDataFirstRow( "mary_goods_attr", new Dictionary<string, object> { { "goods_id", strgoods_id }, { "attr_id", strattr_id } });

                
            if (drData == null)
            {
                Add(dbOrgCon, dbCon, strgoods_id, strattr_id, strattr_value);
                return;
            }
            if (CvtFunc.ToString(drData["attr_value"]).Equals(strattr_value, StringComparison.InvariantCultureIgnoreCase))
            {
                return;
            }

            string strSql = "update mary_goods_attr set attr_value=?attr_value where goods_id=?goods_id and attr_id=?attr_id";
            MySqlParameter[] arrParam = new MySqlParameter[] { 
                new MySqlParameter("?attr_value", strattr_value)     
                ,new MySqlParameter("?goods_id", strgoods_id) 
                ,new MySqlParameter("?attr_id", strattr_id) 
            };
            dbCon.ExecuteSql( strSql, arrParam);
            
        }

        private void Add(DBAccess dbOrgCon, DBAccess dbCon, string strgoods_id, string strattr_id, string strattr_value)
        {
            string strSql = "insert into mary_goods_attr(goods_id,attr_id,attr_value) values(?goods_id,?attr_id,?attr_value)";
            MySqlParameter[] arrParam = new MySqlParameter[] { 
                new MySqlParameter("?goods_id", strgoods_id) 
                ,new MySqlParameter("?attr_id", strattr_id) 
                ,new MySqlParameter("?attr_value", strattr_value) 
            };
            dbCon.ExecuteSql( strSql, arrParam);
        }

       
        private void Delete( DBAccess dbCon, string strgoods_id, string strattr_id)
        {
            string strSql = "delete from mary_goods_attr where goods_id=?goods_id and attr_id=?attr_id";
            MySqlParameter[] arrParam = new MySqlParameter[] { 
                new MySqlParameter("?goods_id", strgoods_id) 
                ,new MySqlParameter("?attr_id", strattr_id) 
            };
            dbCon.ExecuteSql( strSql, arrParam);
        }
    }
}
